const util = require('../../../../utils/util.js')
const http = require('../../../../utils/http.js');
const Net = require('../../../../Net.js');
/** utf-8转换中文 */
function decodeUTF8(str) {
  try {
    return unescape(str.replace(/&#x/g, '%u').replace(/\\u/g, '%u').replace(/;/g, ''));
  } catch (error) {}
}
Page({
  data: {
    title: '商品详情',
    titleType: false,
    hideBottom: true,
    setInter: '',
    annoType: false,
    imgUrls: [],
    isShows: true,
    shopInfo: {
      shop_name: '',
      shop_img: '',
      shop_color: '',
      shop_size: '',
      shop_num: ''
    },
    showModalStatus: false,
    circular: true,
    sku_ids: '',
    shop_id: '',
    autoplay: true,
    interval: 5000,
    duration: 300,
    swiperCurrent: 0,
    modalInfo: '',
    // 使用data数据对象设置样式名
    minusStatus: 'disabled',
    //距离高度
    ScrollFlag: false,
    isShow: false,
    auto: {},
    statusBarHeight: getApp().globalData.statusBarHeight + 4,
    boxhide: true,
    onShareUser: false,
    userStatus: '',
    coupon_end_time: '00:00:00',
    onUrlPlus: false,
    tkl: '',
    onGoodsType: 'pin',
    onShareStatus: '',
    shareViewStatus: true,
    optionValue: {}
  },
  //切换选项卡  
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  Ctrlc(e) {
    var _this = this
    wx.setClipboardData({
      data: '馥致淘口令' + e.currentTarget.id + '去tao寳下单',
      success(res) {
        wx.getClipboardData({
          success(res) {
            _this.setData({
              onShareUser: false,
              showModalStatus: false
            })
          }
        })
      }
    })
  },
  onBindPlus() {
    var userStatus = wx.getStorageSync('token')
    if (userStatus) {
      var userPuls = wx.getStorageSync('member')
      if (userPuls.payment) {
        wx.navigateTo({
          url: '/pages/onNewPages/onPlusCip/onPlusCip',
        })
      } else {
        wx.navigateTo({
          url: '/pages/onNewPages/onUserOutPlus/onUserOutPlus?type=' + this.data.detail.vip.link.type,
        })
      }
    } else {
      wx.navigateTo({
        url: '/pages/Login/Index/index',
      })
    }
  },
  BoxHide(e) {
    var _this = this,
      id = e.currentTarget.id
    if (id == 1) {
      _this.setData({
        boxhide: true
      })
    } else {
      _this.setData({
        shareViewStatus: !_this.data.shareViewStatus
      })
    }
  },
  GetRule() {
    this.setData({
      boxhide: false
    })
  },
  EnterShop() {
    wx.switchTab({
      url: '/tabbarPage/index/index',
    })
  },
  //返回上级
  back() {
    var _this = this
    wx.switchTab({
      url: '/tabbarPage/index/index',
    })
  },
  onGotuBuyGoods() {
    var _this = this,
      user = wx.getStorageSync('token') ? true : false
    if (!user) {
      _this.onUserLogin()
    }
  },
  //图片预览
  preview(e) {
    let index = e.currentTarget.dataset.index;
    let photos = this.data.detail.small_images;
    wx.previewImage({
      current: photos[index], // 当前显示图片的http链接
      urls: photos,
    })
  },
  onPrivetle(e) {
    var id = e.currentTarget.id
    wx.previewImage({
      current: 'https:' + id, // 当前显示图片的http链接
      urls: ['https:' + id],
    })
  },
  onLoad: function (options) {
    var _this = this
    console.log(options)
    try {
      var optionValue = JSON.parse(decodeUTF8(options.q).split('?')[1].split('=')[1])
      console.log(optionValue)
    } catch (error) {
      var optionValue = {
        is: 0
      }
    }
    if (optionValue.sh == 's' || options.type == 'h') {
      _this.setData({
        onShareStatus: true
      })
      if (options.type == 'h') {
        optionValue = {
          is: 1,
          tp: '2',
          pid: options.pid,
          id: options.id,
          type: options.type
        }
      }
      console.log('赋值成功')
      _this.setData({
        optionValue: optionValue
      })
    }
    if (optionValue.is == 1) {
      if (optionValue.tp == '0' || optionValue.tp == '1') {
        _this.setData({
          onGoodsType: 'tao',
          onShareStatus: true,
          tkl: optionValue.kl
        })
      } else {
        _this.setData({
          onGoodsType: 'pin',
          onShareStatus: true
        })
      }
      _this.setData({
        onShareUser: true
      })
    }
    if (options.detail) {
      wx.setStorageSync('detail', {
        tao_id: options.detail
      })
      //是否分享进来的
      var userStatus = wx.getStorageSync('token')
      _this.setData({
        onShareUser: true,
        onUserLoginStatus: wx.getStorageSync('token') ? true : false,
        userStatus: userStatus
      })
    }
    var status = wx.getStorageSync('member')
    if (status.payment) {
      _this.setData({
        onUrlPlus: true
      })
    } else {
      _this.setData({
        onUrlPlus: false
      })
    }
    console.log('1')
  },
  hideModal() {
    var _this = this
    _this.setData({
      showModalStatus: !_this.data.showModalStatus
    })
  },
  onReady: function () {},
  onShow: function () {
    var _this = this
    if (!_this.data.onShareUser) {
      var listinfo = wx.getStorageSync('detail')
      try {
        listinfo.small_images = listinfo.small_images.split('|')
      } catch (error) {
        console.log(listinfo.small_images)
        listinfo.small_images = listinfo.small_images
      }
      _this.setData({
        detail: listinfo,
        onUserLoginStatus: wx.getStorageSync('token') ? true : false
      })
      var data = {
        id: listinfo.tao_id,
        user_type: 2
      }
    } else {
      if (_this.options.q || _this.options.type == 'h') {
        var optionValue = _this.data.optionValue
        console.log(optionValue)
        var listinfo = {
          tao_id: optionValue.id
        }
        var data = {
          id: optionValue.id,
          is_scan: optionValue.is,
          relation_id: optionValue.pid,
          user_type: optionValue.tp
        }
      } else {
        var listinfo = {
          tao_id: _this.options.detail
        }
        var data = {
          id: listinfo.tao_id,
          user_type: 2
        }
      }
    }
    Net.link('v1/goods/' + listinfo.tao_id, data,
      function (res) {
        var time = res.data.data.coupon_end_time
        var repTime = time.replace(/-/g, '/');
        var timeTamp = Date.parse(repTime); // 到期时间戳
        _this.onSettImeOut(timeTamp)
        res.data.data.small_images = res.data.data.small_images.split('|')
        res.data.data.pcDescContent = res.data.data.pcDescContent.split('|')
        wx.setStorageSync('detail', res.data.data)
        _this.setData({
          detail: res.data.data,
          onUserLoginStatus: wx.getStorageSync('token') ? true : false
        })
      }, 'GET',
      function (err) {
        wx.showModal({
          content: err.msg,
          showCancel: false,
          success: (result) => {
            if (result.confirm) {
              wx.switchTab({
                url: '/tabbarPage/index/index',
              })
            }
          },
          title: '温馨提示',
        })
      })
  },
  onSettImeOut(e) {
    var daojishi = util.getTimeLeft(e, 'h:m:s'),
      _this = this
    _this.setData({
      coupon_end_time: daojishi
    })
    if (daojishi != "00:00:00") {
      setTimeout(() => {
        _this.onSettImeOut(e)
      }, 1000);
    }
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function (res) {
    console.log(res)
    var _this = this
    if (res.from != "menu") {
      var info = wx.getStorageSync('detail')
      console.log(wx.getStorageSync('detail'))
      return {
        title: _this.data.detail.tao_title,
        path: '/pages/onNewPages/Shop/Detail/Detail?id=' + info.tao_id + '&type=h' + '&is=1' + '&pid=' + wx.getStorageSync('member').member_pdd_oauth.relation_id + '&tp=2',
        imgUrls: _this.data.detail.small_images[0]
      }
    }else{
      console.log('跳转首页分享')
      return {
        title: '喜赠小程序',
        path: '/tabbarPage/index/index',
        imgUrls: _this.data.detail.small_images[0]
      }
    }
  },
  //去登陆
  onUserLogin(e) {
    wx.navigateTo({
      url: '/pages/Login/Index/index',
    })
  },
})